package com.logictech.lims.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logictech.activity.system.dao.SystemUserDao;
import com.logictech.common.util.data.StringUtils;
import com.logictech.lims.dao.ReagentInventoryDao;
import com.logictech.lims.dto.ReagentInventoryDto;
import com.logictech.lims.entity.ReagentInventory;
import com.logictech.lims.service.LogService;
import com.logictech.lims.service.ReagentInventoryService;

@Service("reagentInventoryService")
public class ReagentInventoryServiceImpl implements ReagentInventoryService {
	@Resource
	private ReagentInventoryDao reagentInventoryDao;
	
	@Resource
	private LogService logService;
	
	@Autowired
	private SystemUserDao systemUserDao;

	@Override
	public void doAdd(ReagentInventory reagentInventory) {
		reagentInventoryDao.add(reagentInventory);
		logService.doAdd("1",reagentInventory);
	}

	@Override
	public void doEdit(ReagentInventory reagentInventory) {
		reagentInventoryDao.update(reagentInventory);
		logService.doAdd("2",reagentInventory);
	}

	@Override
	public void doDelete(String id) {
		ReagentInventory reagentInventory = reagentInventoryDao.get(id);
		if(!StringUtils.isEmpty(reagentInventory.getOpener())){
			String openerVal = systemUserDao.get(reagentInventory.getOpener()).getUserName();
			reagentInventory.setOpenerVal(openerVal);
		}
		logService.doAdd("3",reagentInventory);
		reagentInventoryDao.delete(id);
	}

	@Override
	public ReagentInventoryDto queryById(String id) {
		ReagentInventoryDto reagentInventory  = (ReagentInventoryDto)reagentInventoryDao.get(id);
		return reagentInventory;
	}

	@Override
	public PageList<ReagentInventoryDto> queryPageList(
		PageQuery<ReagentInventoryDto> pageQuery) {
		PageList<ReagentInventoryDto> result = new PageList<ReagentInventoryDto>();
		Integer itemCount = reagentInventoryDao.count(pageQuery);
		List<ReagentInventoryDto> list = reagentInventoryDao.queryPageList(pageQuery,itemCount);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}

	@Override
	public Integer getMaxNo(String no) {
		return reagentInventoryDao.getMaxNo(no);
	}

	@Override
	public ReagentInventoryDto queryByNo(String barCode) {
		return reagentInventoryDao.queryByNo(barCode);
	}

	@Override
	public List<ReagentInventoryDto> queryList(PageQuery<ReagentInventoryDto> pageQuery) {
		return reagentInventoryDao.queryList(pageQuery);
	}

	@Override
	public void startPrint(String barCode) {
		ReagentInventory reagentInventory = reagentInventoryDao.queryByNo(barCode);
		if(!"0".equals(reagentInventory.getPrintStatus())){
			reagentInventory.setPrintStatus("0");
			reagentInventoryDao.update(reagentInventory);
		}
	}

	@Override
	public List<ReagentInventoryDto> getToPrintList() {
		return reagentInventoryDao.getToPrintList();
	}

	@Override
	public void finishPrint(String barCode) {
		ReagentInventory reagentInventory = reagentInventoryDao.queryByNo(barCode);
		if("0".equals(reagentInventory.getPrintStatus())){
			reagentInventory.setPrintStatus("1");
			reagentInventoryDao.update(reagentInventory);
		}
	}

	@Override
	public ReagentInventoryDto getMinEffectiveDate(Integer reagentTypeId) {
		return reagentInventoryDao.getMinEffectiveDate(reagentTypeId).get(0);
	}
}
